import 'dart:io';

import 'package:aero_frisbee/app/model/af_edit_user_param_model/af_edit_user_param_model.dart';
import 'package:aero_frisbee/app/model/af_user_info_model/af_user_info_model.dart';
import 'package:aero_frisbee/app/req/af_req_api.dart';
import 'package:aero_frisbee/app/state/app_cache_state.dart';
import 'package:aero_frisbee/core/state/view_state.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

part 'af_profile_view_model.g.dart';

@riverpod
class CurEditUserParamService extends _$CurEditUserParamService {
  @override
  AfEditUserParamModel build() {
    return AfEditUserParamModel();
  }

  updateAvatar(String avatar) {
    state = state.copyWith(avatarPath: avatar);
  }

  updateBirthDay(String birthDay) {
    state = state.copyWith(birthDate: birthDay);
  }

  updateName(String name) {
    state = state.copyWith(name: name);
  }
}

extension AfEditUserParamModelExt on AfEditUserParamModel {
  bool get isEdited => avatarPath != null || name != null || birthDate != null;
}

@riverpod
class UpdateProfileSubmitService extends _$UpdateProfileSubmitService {
  @override
  ViewState<bool> build() {
    return const ViewState.idle();
  }

  updateProfileSubmit(
      {required AfEditUserParamModel param,
      required AfUserInfoModel userInfo}) async {
    final api = ref.watch(appApiProvider);
    state = const ViewState.loading();
    try {
      if (param.avatarPath != null && param.avatarPath!.isNotEmpty) {
        ///update avatar
        final ossPolicy = await api.getOssModelFetchApiModel();
        final photoPath = await api.uploadPhoto(
            userOssPolicyModel: ossPolicy, file: File(param.avatarPath!));
        await api.updateAvatar(avatarPath: photoPath);
      }

      if (param.birthDate != null &&
          param.birthDate!.isNotEmpty &&
          param.name != null &&
          param.name!.isNotEmpty) {
        ///update birthday
        await api.updateUserInfo(params: {
          "birthday": param.birthDate,
          "about": userInfo.about ?? '',
          "country": userInfo.country ?? '',
          "lauguage": userInfo.language ?? '',
          "nickname": param.name ?? userInfo.nickname ?? ''
        });
      } else if (param.birthDate != null && param.birthDate!.isNotEmpty) {
        ///update birthday
        await api.updateUserInfo(params: {
          "birthday": param.birthDate,
          "about": userInfo.about ?? '',
          "country": userInfo.country ?? '',
          "lauguage": userInfo.language ?? '',
          "nickname": param.name ?? userInfo.nickname ?? ''
        });
      } else if (param.name != null && param.name!.isNotEmpty) {
        await api.updateUserInfo(params: {
          "birthday": userInfo.birthday,
          "about": userInfo.about ?? '',
          "country": userInfo.country ?? '',
          "lauguage": userInfo.language ?? '',
          "nickname": param.name ?? userInfo.nickname ?? ''
        });
      }
      state = const ViewState.ready(true);
    } catch (e) {
      state = ViewState.error(error: e.toString());
    }
  }
}

@riverpod
class DeleteAccountService extends _$DeleteAccountService {
  @override
  ViewState<bool> build() {
    return const ViewState.idle();
  }

  deleteAccount() async {
    final api = ref.watch(appApiProvider);
    state = const ViewState.loading();
    try {
      final engine = ref.read(curEngineProvider);
      await engine?.disconnect(false);
      await engine?.destroy();
      final model = await api.deleteAccount();
      state = ViewState.ready(model);
    } catch (e) {
      state = ViewState.error(error: e.toString());
    }
  }
}

@riverpod
class LogoutService extends _$LogoutService {
  @override
  ViewState<bool> build() {
    return const ViewState.idle();
  }

  logout() async {
    final api = ref.watch(appApiProvider);
    state = const ViewState.loading();
    try {
      final model = await api.logout();
      state = ViewState.ready(model);
    } catch (e) {
      state = ViewState.error(error: e.toString());
    }
  }
}
